-- Scorchio! 2, A multi-mob vulnerability manager
-- Copyright (C) 2008-2009  ennui@bloodhoof-eu, ennuilg@gmail.com
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 3
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.

-- GLOBALS: pairs

-- Upgrading the database is in the Options UI, because the logic here could grow very large, especially
-- if it starts including old defaults tables.
function Scorchio2:UpgradeDatabase(sv, DBVERSION)
	-- TODO: upgrade functions should be nil'd at some point. Probably by InitOptions though.
	local oldVersion = sv.dbversion or 0
	if oldVersion < 1 then
		-- Version 1 cleaned up unused Winter's Chill and Arcane Blast options.
		for _,prof in pairs(sv.profiles or {}) do
			-- Winter's Chill removed
			if prof.baroptions then
				prof.baroptions.b = nil
			end
			-- Many Arcane Blast options removed
			if prof.baroptions and prof.baroptions.i then
				local i = prof.baroptions.i
				i.message = nil
				i.warning = nil
				i.showwarning = nil
				i.showexpire = nil
				i.warningtime = nil
				i.soundson = nil
				i.procsound = nil
				i.warningsound = nil
				i.expiredsound = nil
			end
		end
	end
	if oldVersion < 2 then
		-- Version 2 cleans up anchoroptions and baroptions, so that all children have similar semenatics. This
		-- will let us use AceDB's inheritence features in the new defaults table.
		for _,prof in pairs(sv.profiles or {}) do
			-- Very old versions stored the anchor data in baroptions, back when there was only a single anchor.
			-- It's totally unused now and should be stripped from SVs.
			if prof.baroptions then
				for k,_ in pairs(prof.baroptions) do
					-- Remove any key with length > 1. The exact list of keys, if anyone cares, can be found in
					-- v1.0.4's defaults table. But this works just as well and is clearer.
					if #k > 1 then
						prof.baroptions[k] = nil
					end
				end
			end
			-- Move "custom anchors" setting out of anchoroptions, since it's nothing like anchoroptions' other children.
			if prof.anchoroptions then
				prof.custom_anchors = prof.anchoroptions.custom
				prof.anchoroptions.custom = nil
			end
		end
	end
	if oldVersion < DBVERSION then
		sv.dbversion = DBVERSION
	end
end
